package com.saret.translate.type;

import com.saret.utils.Consumer;
import com.saret.utils.UtfFileHandle;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author biniam.gebremichael
 * @since 11/25/11
 */
public class MyDictionary {
    final Map<String, MyDictionaryEntry> dictionary = new HashMap<String, MyDictionaryEntry>();

    public MyDictionary() {
    }

    public MyDictionary(File dicFile) {
         try {
            UtfFileHandle.readFile(dicFile, new Consumer<String>() {
                @Override
                public void consume(String s) {
                    String[] line = s.split(",", 2);
                    MyDictionaryEntry value = new MyDictionaryEntry(line[0], line[1]);
                    dictionary.put(value.getEnglish(), value);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Map<String, MyDictionaryEntry> getDictionary() {
        return dictionary;
    }

    public MyDictionary translate(String... s) {
        MyDictionary translated = new MyDictionary();
        for (String s1 : s) {
            MyDictionaryEntry entry = dictionary.get(s1);
            if (entry != null) {
                translated.getDictionary().put(entry.getEnglish(),entry);
            }
        }
        return translated;
    }

      public MyDictionary reverseTranslate( String... s) {
        MyDictionary translated = new MyDictionary();
        for (String s1 : s) {
            for (final MyDictionaryEntry value : dictionary.values()) {
                if(s1.length()>1 && value.tigrignaIndexOf(s1)>=0){
               //     System.out.println("value = " + GeezUtil.toLatin(value.getTigrigna()));
                    translated.getDictionary().put(value.getEnglish(),value);
                }
            }
        }
        return translated;
    }
}
